//
//  ZGrantDetailDealVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/17.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZGrantDetailDealVC.h"

#import "ZGrantStateCell.h"
#import "BApplyForMoneyDetailCell.h"
#import "BApplyForMoneyReasonCell.h"
#import "ZGrantDealCell.h"

#import "ZGrantDetailModel.h"

#import "ZGrantDetailDealDisagreeVC.h"

#import "ZGrantCheckDetailVC.h"//补助金详情

@interface ZGrantDetailDealVC () <UITableViewDelegate,UITableViewDataSource,ZGrantDealCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZGrantDetailModel *grantDetailModel;

@end

@implementation ZGrantDetailDealVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"查看详情";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = kColorWithHex(0xf2f2f2);
    
    [self setupViewAction];
    
    [self requestData];
}
#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ZGrantStateCell class] forCellReuseIdentifier:NSStringFromClass([ZGrantStateCell class])];
    [_tableView registerClass:[BApplyForMoneyDetailCell class] forCellReuseIdentifier:NSStringFromClass([BApplyForMoneyDetailCell class])];
    [_tableView registerClass:[BApplyForMoneyReasonCell class] forCellReuseIdentifier:NSStringFromClass([BApplyForMoneyReasonCell class])];
    [_tableView registerClass:[ZGrantDealCell class] forCellReuseIdentifier:NSStringFromClass([ZGrantDealCell class])];
    
}
#pragma mark - 数据请求
- (void)requestData{
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObjectIfNotNil:self.grantID forKey:@"id"];
    [NetWorkTool POST:USER_GRANTS_INFO param:param success:^(id dic) {
        weakSelf.grantDetailModel = [ZGrantDetailModel mj_objectWithKeyValues:[dic objectForKey:@"data"]];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.grantDetailModel) {
        return 4;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //省级
    if (indexPath.row == 0) {
        ZGrantStateCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZGrantStateCell class]) forIndexPath:indexPath];
        cell.grantDetailModel = self.grantDetailModel;
        return cell;
    }else if (indexPath.row == 1) {
        BApplyForMoneyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BApplyForMoneyDetailCell class]) forIndexPath:indexPath];
        cell.grantDetailModel = self.grantDetailModel;
        return cell;
    }else if (indexPath.row == 2){
        BApplyForMoneyReasonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BApplyForMoneyReasonCell class]) forIndexPath:indexPath];
        cell.isGrantDetail = YES;
        cell.grantDetailModel = self.grantDetailModel;
        return cell;
    }else if (indexPath.row == 3){
        ZGrantDealCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZGrantDealCell class]) forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    
    return [UITableViewCell new];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return [ZGrantStateCell cellHeightNormal];
    }else if (indexPath.row == 1) {
        return [BApplyForMoneyDetailCell cellHeight];
    }else if (indexPath.row == 2){
        return [BApplyForMoneyReasonCell cellHeight];
    }else if (indexPath.row == 3){
        return [ZGrantDealCell cellHeight];
    }
    
    return 0;
}

#pragma mark - ZGrantDealCellDelegate
- (void)grantDealCellBtnClick:(NSInteger)tag{
    if (tag == 200) {
        //不通过
        ZGrantDetailDealDisagreeVC *vc = [ZGrantDetailDealDisagreeVC new];
        vc.grantID = self.grantID;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        //通过
        kWeakSelf;
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObjectIfNotNil:@"1" forKey:@"agree"];
        [param setObjectIfNotNil:self.grantDetailModel.Id forKey:@"id"];
        [param setObjectIfNotNil:@"" forKey:@"reason"];
        [NetWorkTool POST:USER_APPROVAL_GRANTS param:param success:^(id dic) {
            ShowToastWithText(@"审核通过");
            [[NSNotificationCenter defaultCenter] postNotificationName:USER_DEAL_GRANT_SUCCESS object:nil];
            ZGrantCheckDetailVC *vc = [[ZGrantCheckDetailVC alloc] init];
            vc.grantID = self.grantID;
            vc.isFromDeal = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
        } fail:^(NSError *error) {
            
        } needUser:YES];
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
